package com.business_clean.viewmodel.request

import androidx.lifecycle.MutableLiveData
import com.business_clean.app.config.Constant
import com.business_clean.app.network.NetUrl
import com.business_clean.data.mode.init.InitDataEntity
import com.business_clean.data.mode.login.UserInfo
import com.business_clean.data.mode.todo.TodoList
import me.hgj.mvvmhelper.base.BaseViewModel
import me.hgj.mvvmhelper.ext.rxHttpRequest
import me.hgj.mvvmhelper.net.LoadingType
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toResponse

/**
 * 在main中需要获取的内容
 */
class MainViewModel : BaseViewModel() {

    var initData = MutableLiveData<InitDataEntity>()

    var userInfo = MutableLiveData<UserInfo>()

    var todoList = MutableLiveData<TodoList>()

    //获取源数据
    fun requestInitData() {
        rxHttpRequest {
            onRequest = {
                initData.value = RxHttp.get(NetUrl.GET_INIT_DATA)
                    .toResponse<InitDataEntity>().await()
            }
        }
    }

    /**
     * 获取个人信息
     */
    fun requestUserInfo() {
        rxHttpRequest {
            onRequest = {
                userInfo.value = RxHttp.get(NetUrl.GET_USER_INFO)
                    .toResponse<UserInfo>().await()
            }
        }
    }


    /**
     * 获取列表
     */
    fun requestTodoList(page: Int, type: Int) {

        rxHttpRequest {
            onRequest = {
                todoList.value = RxHttp.get(NetUrl.GET_TODO_LIST)
                    .add("page", page)
                    .add("size", Constant.PAGE_SIZE)
                    .add("type", type)
                    .toResponse<TodoList>().await()
            }
            loadingType = LoadingType.LOADING_NULL
        }

    }
}